home *** CD-ROM | disk | FTP | other *** search
/ Shareware Super Platinum 8 / Shareware Super Platinum 8.iso / mac / WIN_PRO / VIEWS.ZIP;1 / CVDZIP.EXE / CVCOLOR.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-22  |  4.2 KB  |  241 lines

  1. /*
  2.     cvcolor.cpp
  3.  
  4.     Custom color control
  5.     
  6.     C++/Views 2.0 Demo
  7.     Copyright (c) 1992, by Liant Software Corp.
  8.     ALL RIGHTS RESERVED.
  9.  
  10.     Revision History:
  11.     -----------------
  12. */
  13.  
  14. #include "cvcolor.h"
  15. #include "notifier.h"
  16. #include "port.h"
  17. #include "brush.h"
  18. #include "pen.h"
  19.  
  20. defineClass(ColorControl, VControl)
  21.  
  22. rgbColor colors[16];
  23.  
  24. ColorControl::ColorControl()
  25. {
  26.     ;
  27. }
  28.  
  29. ColorControl::ColorControl(VFrame &frame, VWindow *parent, short style)
  30.     : VControl(frame, parent, style)
  31. {
  32.     currColor = 4;
  33.     clickMthd = 0;
  34.  
  35.     // initialize colors
  36.     colors[ 0] = RGBCLR(  0,   0,   0);
  37.     colors[ 1] = RGBCLR(128,   0,   0);
  38.     colors[ 2] = RGBCLR(  0, 128,   0);
  39.     colors[ 3] = RGBCLR(  0,   0, 128);
  40.     colors[ 4] = RGBCLR(128, 128,   0);
  41.     colors[ 5] = RGBCLR(128,   0, 128);
  42.     colors[ 6] = RGBCLR(  0, 128, 128);
  43.     colors[ 7] = RGBCLR(128, 128, 128);
  44.     colors[ 8] = RGBCLR( 96,  96,  96);
  45.     colors[ 9] = RGBCLR(255,   0,   0);
  46.     colors[10] = RGBCLR(  0, 255,   0);
  47.     colors[11] = RGBCLR(  0,   0, 255);
  48.     colors[12] = RGBCLR(255, 255,   0);
  49.     colors[13] = RGBCLR(255,   0, 255);
  50.     colors[14] = RGBCLR(  0, 255, 255);
  51.     colors[15] = RGBCLR(255, 255, 255);
  52. }
  53.  
  54. ColorControl::~ColorControl()
  55. {
  56.     ;
  57. }
  58.  
  59. boolean ColorControl::free()
  60. {
  61.     delete this;
  62.     return(TRUE);
  63. }
  64.  
  65. boolean ColorControl::mouseDn(int mx, int my)
  66. /*
  67.     mouse button pressed in the control
  68. */
  69.     int clr;
  70.  
  71.     notifier->mouseTracking(TRUE);
  72.     notifier->captureMouseFor(this);
  73.  
  74.     if (getMouseColor(mx, my, &clr)) {
  75.         setColor(clr);
  76.     }
  77.  
  78.     return(TRUE);
  79. }
  80.  
  81. boolean ColorControl::mouseMv(int mx, int my, int bStat) 
  82. /*
  83.     Called when the mouse moves pressed in our window
  84.     (and mouse tracking is turned on).
  85. */
  86. {
  87.     int clr;
  88.  
  89.     if (bStat && getMouseColor(mx, my, &clr)) {
  90.         setColor(clr);
  91.     }
  92.  
  93.     return(TRUE);
  94. }
  95.  
  96. boolean ColorControl::mouseUp(int mx, int my)
  97. /*
  98.     mouse button released in the control
  99. */
  100. {
  101.     int clr;
  102.  
  103.     if (getMouseColor(mx, my, &clr)) {
  104.         setColor(clr);
  105.     }
  106.  
  107.     /* invoke callback method */
  108.     if (client && clickMthd != NIL_METHOD) {
  109.         client->perform(clickMthd);
  110.     }
  111.  
  112.     notifier->mouseTracking(FALSE);
  113.     notifier->releaseCapture();
  114.     return(TRUE);
  115. }
  116.  
  117. boolean ColorControl::getMouseColor(int mx, int my, int *clr)
  118. /*
  119.     Determine which color cell the mouse is pointing to.
  120.     This function is used by the mouse routines.
  121. */
  122. {
  123.     int x, y, w, h;
  124.     int row, col;
  125.     getArea(&x, &y, &w, &h);
  126.  
  127.     row = my / (h / 2);
  128.     col = mx / (w / 8);
  129.  
  130.     *clr = (row * 8) + col;
  131.  
  132.     return(TRUE);
  133. }
  134.  
  135. void ColorControl::setColor(int clr)
  136. /*
  137.     Highlight a new cell and update the window.
  138. */
  139. {
  140.     int oldclr = currColor;
  141.     currColor = clr;
  142.  
  143.     if (oldclr != currColor) {
  144.         int x, y, w, h;
  145.         int row, col;
  146.         getArea(&x, &y, &w, &h);
  147.  
  148.         VRectangle    drawbox;
  149.  
  150.         /* repaint old cell */
  151.         col  = oldclr % 8;
  152.         row  = oldclr / 8;
  153.         drawbox.set(CornerDim, (col * w) / 8, (row * h) / 2, w / 8, h / 2);
  154.         update(&drawbox);
  155.  
  156.         /* repaint new cell */
  157.         col  = currColor % 8;
  158.         row  = currColor / 8;
  159.         drawbox.set(CornerDim, (col * w) / 8, (row * h) / 2, w / 8, h / 2);
  160.         update(&drawbox);
  161.     }
  162. }
  163.  
  164. boolean ColorControl::paint() 
  165. /*
  166.     Draw the color control.
  167. */
  168. {
  169.     VPen     pn;    
  170.     VBrush     br;
  171.     VPort     p(this);
  172.  
  173.     p.open();
  174.     p.usePen(&pn);
  175.     p.useBrush(&br);
  176.  
  177.     //draw color rectangles
  178.     int x, y, w, h;
  179.     int row, col;
  180.     getArea(&x, &y, &w, &h);
  181.  
  182.     VRectangle r;
  183.  
  184.     for (row = 0; row < 2; row++) {
  185.         for (col = 0; col < 8; col++) {
  186.  
  187.             r.set(CornerDim, (col * w) / 8, (row * h) / 2, w / 8, h / 2);
  188.  
  189.             if ((row * 8 + col) == currColor) {
  190.                 /* highlight current color */
  191.                 pn.color((currColor == 15) ? RED : WHITE);
  192.             }
  193.             else {
  194.                 pn.color(BLACK);
  195.             }
  196.  
  197.             br.foreground(colors[row * 8 + col]);
  198.  
  199.             p.fillRegion(&r);
  200.         }
  201.     }
  202.  
  203.     p.useFont(NIL);
  204.     p.usePen(NIL);
  205.     p.close();
  206.     
  207.     return(TRUE);
  208. }
  209.  
  210. boolean ColorControl::erased()
  211. /*
  212.     Intercept painting of window background.
  213. */
  214. {
  215.     return(TRUE);
  216. }
  217.  
  218. rgbColor ColorControl::getColor()
  219. /*
  220.     Return the currently selected color value
  221. */
  222. {
  223.     return(colors[currColor]);
  224. }
  225.  
  226. void ColorControl::uponClick(id clnt, method mthd) 
  227. {
  228.     if (clnt) {
  229.         client = clnt; 
  230.     }
  231.     if (mthd) {
  232.         clickMthd = mthd; 
  233.     }
  234.     if (!clnt || mthd == NIL_METHOD) {
  235.         client = NIL;
  236.         clickMthd = NIL;
  237.     }
  238. }
  239.  
  240.